<?php
namespace app\admin\controller;

use app\common\controller\Adminbase;
use app\admin\model\Procedure as ProcedureModel;
use app\admin\model\ProductRoute as ProductRouteModel;
use think\Db;
class ProductRoute extends Adminbase
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $where = [];
            $page = $this->request->param('page');
            $limit = $this->request->param('limit');
            if($this->request->param('filter')){
                $filter = $this->request->param('filter');
                $filter = json_decode($filter,true);
                foreach($filter as $k=>$row){
                    $where[] = [$k,'like','%'.trim($row).'%'];
                }
            }
            $offset = ($page - 1)*$limit;
            $result = ProductRouteModel::where('deleted',0)->where($where)->limit($offset,$limit)->select();
            foreach($result as $k=>$row){
                $procedure_ids = json_decode($row['procedure_ids']);
                $name = '';
                foreach($procedure_ids as $kk=>$v){
                    if($v[0] == 1){
                        $name .= ProcedureModel::where('deleted',0)->find($v[1])['title'].',';
                    }else if($v[0] == 2){
                        $name .= ProductRouteModel::where('deleted',0)->find($v[1])['title'].',';
                    }
                }
                $name = rtrim($name,',');
                $result[$k]['name'] = $name;
            }
            $count = ProductRouteModel::where('deleted',0)->where($where)->count();
            $result = array("code" => 0,  'count'=>$count,"data" => $result);
            return json($result);
        }
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id');
        $productRoute = ProductRouteModel::where('deleted',0)->find($id);
        $procedure_ids = json_decode($productRoute['procedure_ids'],true);
        foreach($procedure_ids as $k=>$row){
            if($row[0] == 1){
                $procedure_ids[$k][2] = ProcedureModel::where('deleted',0)->find($row[1])['title'];
            }else if($row[0] == 2){
                $procedure_ids[$k][2] = ProductRouteModel::where('deleted',0)->find($row[1])['title'];
            }
        }
        
        $procedureList = ProcedureModel::where('deleted',0)->select();
        // dg($procedureList);
        $productRouteList = ProductRouteModel::where('deleted',0)->select();

        $productRoute['procedure_ids'] = $procedure_ids;
        $this->assign('productRoute',$productRoute);
        $this->assign('procedureList',$procedureList);
        $this->assign('productRouteList',$productRouteList);
        return $this->fetch();
    }


    public function editPost()
    {
        $data = $this->request->param();
        if(isset($data['type'])){
            $procedure_ids = [];
            foreach($data['type'] as $k=>$row){
                $procedure_ids[] = [$row,$data['procedure'][$k]];
            }
        }
        $data['procedure_ids'] = json_encode($procedure_ids);
        unset($data['procedure']);
        unset($data['type']);
        if(!isset($data['id'])){
            $check = ProductRouteModel::where('nums',$data['nums'])->find();
            if(!empty($check)){
                $this->error('工艺路线编号重复了');
            }
            $data['addtime'] = date('Y-m-d H:i:s');
            ProductRouteModel::insert($data);
        }else{
            unset($data['nums']);
            $data['mdtime'] = date('Y-m-d H:i:s');
            ProductRouteModel::where('id',$data['id'])->update($data);
        }
        $this->success('添加成功');
    }

    
    public function getProcedure()
    {
        $type = $this->request->param('type');
        if($type == 1){
            $data = ProcedureModel::where('deleted',0)->select();
        }else{
            $data = ProductRouteModel::where('deleted',0)->select();
        }
        return json(['code'=>1,'data'=>$data]);
    }

    
    public function delete()
    {
        $data = $this->request->param();
        ProductRouteModel::where('id',$data['id'])->update(['deleted'=>1]);
        $this->success('删除成功',url('index'));
    }
}